<?php

namespace Xsjosg\Zt\App\Webapp\Sixyear251014\Cpanel\Traits;

use Exception;
use Xsjosg\Zt\App\Webapp\Sixyear251014\Models\ShareUid;
use Xsjosg\Zt\Core\Helper\Misc;
use Xsjosg\Zt\Core\Helper\Str;

trait ShareUidControllerTrait
{
    public function shareUidListAction()
    {
        $options['query'] = ShareUid::slave()->orderBy('id', 'desc');

        $data['id'] = trim(app('request')->input('id'));
        $data['share_uid'] = trim(app('request')->input('share_uid'));
        $data['share_info'] = trim(app('request')->input('share_info'));
        $data['create_time'] = trim(app('request')->input('create_time'));
        $data['update_time'] = trim(app('request')->input('update_time'));

        if (strlen($data['id'])) {
            $options['query']->where('id', $data['id']);
        }
        if (strlen($data['share_uid'])) {
            $options['query']->where('share_uid', $data['share_uid']);
        }
        if (strlen($data['share_info'])) {
            $options['query']->where('share_info', $data['share_info']);
        }
        if (strlen($data['create_time'])) {
            list($beginTime, $endTime) = Str::explodeRangeTime($data['create_time'], true);
            $options['query']->whereBetween('create_time', [$beginTime, $endTime]);
        }
        if (strlen($data['update_time'])) {
            $options['query']->where('update_time', $data['update_time']);
        }

        $options['title'] = '分享uid映射记录';
        $options['listActions'] = [
            [
                'desc' => '全部',
                'icon' => 'fire',
                'link' => '@share_uid_list',
                'target' => '_self',
            ],
            [
                'desc' => '查找',
                'icon' => 'search',
                'link' => '@share_uid_search',
            ],
            [
                'desc' => '导出',
                'icon' => 'export',
                'link' => '@share_uid_export',
                'data' => app('request')->query(),
                'target' => '_self',
            ],
        ];
        $options['fields'] = [
            ['desc' => 'ID', 'name' => 'id'],
            ['desc' => '分享角色id', 'name' => 'share_uid'],
            ['desc' => '分享信息', 'name' => 'share_info'],
            ['desc' => '创建时间', 'name' => 'create_time'],
            ['desc' => '更新时间', 'name' => 'update_time'],
        ];
        $options['formatter'] = function ($row) {
            $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            $row['update_time'] = date('Y-m-d H:i:s', $row['update_time']);
            return array_map('htmlspecialchars', $row);
        };

        return app('cpanel.view')->createListPage($options);
    }

    public function shareUidSearchAction()
    {
        $options['title'] = '查找';
        $options['action'] = '@share_uid_list';
        $options['submit'] = ['form' => true];
        $options['fields'] = [
            ['desc' => 'ID', 'name' => 'id'],
            ['desc' => '分享角色id', 'name' => 'share_uid'],
        ];
        return app('cpanel.view')->createModalForm($options);
    }

    public function shareUidExportAction()
    {
        $query = ShareUid::slave()->orderBy('id', 'asc');

        $data['id'] = trim(app('request')->input('id'));
        $data['share_uid'] = trim(app('request')->input('share_uid'));
        $data['share_info'] = trim(app('request')->input('share_info'));
        $data['create_time'] = trim(app('request')->input('create_time'));
        $data['update_time'] = trim(app('request')->input('update_time'));

        if (strlen($data['id'])) {
            $query->where('id', $data['id']);
        }
        if (strlen($data['share_uid'])) {
            $query->where('share_uid', $data['share_uid']);
        }
        if (strlen($data['share_info'])) {
            $query->where('share_info', $data['share_info']);
        }
        if (strlen($data['create_time'])) {
            list($beginTime, $endTime) = Str::explodeRangeTime($data['create_time'], true);
            $query->whereBetween('create_time', [$beginTime, $endTime]);
        }
        if (strlen($data['update_time'])) {
            $query->where('update_time', $data['update_time']);
        }

        $data['query'] = $query;
        $data['formatter'] = function ($row) {
            $row['create_time'] = date('Y-m-d H:i:s', $row['create_time']);
            $row['update_time'] = date('Y-m-d H:i:s', $row['update_time']);
            return [
                'ID' => $row['id'],
                '分享角色id' => $row['share_uid'],
                '分享信息' => $row['share_info'],
                '创建时间' => $row['create_time'],
                '更新时间' => $row['update_time'],
            ];
        };

        return Misc::exportCsv($data, null, 'share_uid_'.date('Ymd_His').'.csv');
    }
}
